#include <buola/chart/plots/cmeshplot.h>

#include <buola/image/cgc.h>
#include <buola/gl.h>
#include <buola/chart/cxyzchart.h>
#include <buola/chart/c3daxis.h>

namespace buola { namespace chart {

CMeshPlot::CMeshPlot()
    :   mLineColor(std::wstring(L"black")),
        mLineWidth(2)
{
    mStyle=CPlot::MESH;
}

CMeshPlot::~CMeshPlot()
{
}

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
////////////////////////// Interface //////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

void CMeshPlot::SetSeries(P3DSeries pSeries)
{
    mSeries=pSeries;
}

void CMeshPlot::SetColor(const img::CColor &pColor)
{
    mLineColor=pColor;
}

void CMeshPlot::SetLineWidth(double pWidth)
{
    mLineWidth=pWidth;
}

void CMeshPlot::SetLineColor(const img::CColor &pColor)
{
    mLineColor=pColor;
}

void CMeshPlot::SetName(const std::wstring &pName)
{
    if(mSeries)
        mSeries->mName=pName;
}

void CMeshPlot::Draw(CXYZChartContext &pC)
{
#if 0
    if(!mSeries->GetRows()||!mSeries->GetCols()) return;

    if(!(mStyle&DISABLED))
    {
//      glPushMatrix();

        glEnable(GL_DEPTH_TEST);

        glPolygonOffset(1.0,1.0);
        glEnable(GL_POLYGON_OFFSET_FILL);
        //glbColor(CColor(GREEN));

        CColor lColor;

        for(int i=0;i<mSeries->GetRows()-1;i++)
        {
            glBegin(GL_TRIANGLE_STRIP);
            for(int j=0;j<mSeries->GetCols();j++)
            {
                img::CColor lColor;
                lColor.SetHSV(-pC.mZAxis->Map(mSeries->GetZ(i,j))/0.7*120.0+120.0,
                                0.8,0.9);
                //glbColor(lColor);
                glVertex3d(pC.mXAxis->Map(mSeries->GetX(i,j)),
                            pC.mYAxis->Map(mSeries->GetY(i,j)),
                            pC.mZAxis->Map(mSeries->GetZ(i,j)));
                glVertex3d(pC.mXAxis->Map(mSeries->GetX(i+1,j)),
                            pC.mYAxis->Map(mSeries->GetY(i+1,j)),
                            pC.mZAxis->Map(mSeries->GetZ(i+1,j)));
            }
            glEnd();
        }

        glbColor(CColor(BLACK));
        glBegin(GL_LINES);
        for(int i=0;i<mSeries->GetRows();i++)
        {
            for(int j=0;j<mSeries->GetCols()-1;j++)
            {
                glVertex3d(pC.mXAxis->Map(mSeries->GetX(i,j)),
                            pC.mYAxis->Map(mSeries->GetY(i,j)),
                            pC.mZAxis->Map(mSeries->GetZ(i,j)));
                glVertex3d(pC.mXAxis->Map(mSeries->GetX(i,j+1)),
                            pC.mYAxis->Map(mSeries->GetY(i,j+1)),
                            pC.mZAxis->Map(mSeries->GetZ(i,j+1)));
            }
        }
        for(int j=0;j<mSeries->GetCols();j++)
        {
            for(int i=0;i<mSeries->GetRows()-1;i++)
            {
                glVertex3d(pC.mXAxis->Map(mSeries->GetX(i,j)),
                            pC.mYAxis->Map(mSeries->GetY(i,j)),
                            pC.mZAxis->Map(mSeries->GetZ(i,j)));
                glVertex3d(pC.mXAxis->Map(mSeries->GetX(i+1,j)),
                            pC.mYAxis->Map(mSeries->GetY(i+1,j)),
                            pC.mZAxis->Map(mSeries->GetZ(i+1,j)));
            }
        }
        glEnd();

//      glPopMatrix();
    }

    CChartLegend::SItem lItem;
    lItem.mText=mSeries->mName;
    lItem.mColor=mLineColor;
    lItem.mPlot=this;
    lItem.mStyle=mStyle;
    pC.mLegend->AddItem(lItem);
#endif
}

/*namespace chart*/ } /*namespace buola*/ }
